package com.yic.module.device.controller.admin.maintenancePlanEntry;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.maintenancePlanEntry.vo.*;
import com.yic.module.device.dal.dataobject.maintenancePlanEntry.MaintenancePlanEntryDO;
import com.yic.module.device.convert.maintenancePlanEntry.MaintenancePlanEntryConvert;
import com.yic.module.device.service.maintenancePlanEntry.MaintenancePlanEntryService;

@Tag(name = "管理后台 - 设备保养项目")
@RestController
@RequestMapping("/device/maintenance-plan-entry")
@Validated
public class MaintenancePlanEntryController {

    @Resource
    private MaintenancePlanEntryService maintenancePlanEntryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备保养项目")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:create')")
    public CommonResult<Long> createMaintenancePlanEntry(@Valid @RequestBody MaintenancePlanEntryCreateReqVO createReqVO) {
        return success(maintenancePlanEntryService.createMaintenancePlanEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备保养项目")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:update')")
    public CommonResult<Boolean> updateMaintenancePlanEntry(@Valid @RequestBody MaintenancePlanEntryUpdateReqVO updateReqVO) {
        maintenancePlanEntryService.updateMaintenancePlanEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备保养项目")
    @Parameter(name = "id", description = "编号", required = true)
    @Parameter(name = "flag",description = "删除与设备关联信息",required = false)
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:delete')")
    public CommonResult<Boolean> deleteMaintenancePlanEntry(@RequestParam("id") Long id,@RequestParam("flag") Boolean flag) {
        maintenancePlanEntryService.deleteMaintenancePlanEntry(id,flag);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备保养项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:query')")
    public CommonResult<MaintenancePlanEntryRespVO> getMaintenancePlanEntry(@RequestParam("id") Long id) {
        MaintenancePlanEntryDO maintenancePlanEntry = maintenancePlanEntryService.getMaintenancePlanEntry(id);
        return success(MaintenancePlanEntryConvert.INSTANCE.convert(maintenancePlanEntry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备保养项目列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:query')")
    public CommonResult<List<MaintenancePlanEntryRespVO>> getMaintenancePlanEntryList(@RequestParam("ids") Collection<Long> ids) {
        List<MaintenancePlanEntryDO> list = maintenancePlanEntryService.getMaintenancePlanEntryList(ids);
        return success(MaintenancePlanEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备保养项目分页")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:query')")
    public CommonResult<PageResult<MaintenancePlanEntryRespVO>> getMaintenancePlanEntryPage(@Valid MaintenancePlanEntryPageReqVO pageVO) {
        PageResult<MaintenancePlanEntryDO> pageResult = maintenancePlanEntryService.getMaintenancePlanEntryPage(pageVO);
        return success(MaintenancePlanEntryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/not-select-page")
    @Operation(summary = "获得设备保养项目未选择分页")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:query')")
    public CommonResult<PageResult<MaintenancePlanEntryRespVO>> getMaintenancePlanEntryNotSelectPage(@Valid MaintenancePlanEntryPageReqVO pageVO) {
        PageResult<MaintenancePlanEntryDO> pageResult = maintenancePlanEntryService.getMaintenancePlanEntryNotSelectPage(pageVO);
        return success(MaintenancePlanEntryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备保养项目 Excel")
    @PreAuthorize("@ss.hasPermission('device:maintenance-plan-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportMaintenancePlanEntryExcel(@Valid MaintenancePlanEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MaintenancePlanEntryDO> list = maintenancePlanEntryService.getMaintenancePlanEntryList(exportReqVO);
        // 导出 Excel
        List<MaintenancePlanEntryExcelVO> datas = MaintenancePlanEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备保养项目.xls", "数据", MaintenancePlanEntryExcelVO.class, datas);
    }

}
